<?php
class TestingController extends ExtendedController
{
    function __construct()
    {
        parent::__construct();

        if(Session::getInstance()->get('permission') != '3')
        {
            $_SESSION['ref'] = substr($_SERVER['PATH_INFO'],1);
            Utility::error('Bạn không đủ quyền hạn để sử dụng chức năng này', 'login');
            exit();
        }
    }
    
    public function index($message = null)
    {
        $sessionObj = Session::getInstance();
        $sid = $sessionObj->get('id');
        
        $testingModel = $this->loadModel('testing');

        $exam = null;
        $exams = $testingModel->getExamList($sid);

        $examsView = $this->loadView('exams');
        $examsView->appendBlockData(array('exams'=>$exams));
        $examsView->appendBlockData(array('message'=>$message));
        $examsView->setBlockData("Chọn bài thi","main_header");
        $examsView->render();
    }
    
    public function start($classID = null, $subjectID = null, $totalTime = null)
    {
        $sessionObj = Session::getInstance();
        $studentID = $sessionObj->get('id');

        $testingModel = $this->loadModel('testing');
        $message = $testingModel->start($classID, $studentID, $subjectID, $totalTime);
        if ($message != null)
            $this->index($message);
        else
            $this->question($classID, 1);
    }

    public function overview($classID = null)
    {
        $sessionObj = Session::getInstance();
        $studentID = $sessionObj->get('id');
        
        $testingModel = $this->loadModel('testing');
        $questionArr = $testingModel->getQuestions($classID, $studentID);

        if ($questionArr == null)
        {
            $this->index();
            return;
        }

        $tz = new DateTimeZone('Asia/Bangkok');
        $now = new DateTime("now", $tz);
        $results = $testingModel->getResult($classID, $studentID);
        $finishTime = new DateTime($results[0]->finishtime, $tz);
        $remain = $finishTime->diff($now);

        $examInfo = $testingModel->getExamInfo($classID);

        $testingOverView = $this->loadView('overview');
        $testingOverView->appendBlockData(array('questions'=>$questionArr));
        $testingOverView->appendBlockData(array('classID'=>$classID));

        $count = 0;
        $totalCount = 0;
        foreach ($questionArr as $question)
        {
            $totalCount++;
            if ($question->choice != 0) $count++;
        }
        
        $testingOverView->setBlock('block_content', 'info.html', false);
        $testingOverView->appendBlockData(array('examInfo'=>$examInfo), 'block_content');
        $testingOverView->appendBlockData(array('classID'=>$classID), 'block_content');
        $testingOverView->appendBlockData(array('studentID'=>$studentID), 'block_content');
        $testingOverView->appendBlockData(array('now'=>$now), 'block_content');
        $testingOverView->appendBlockData(array('remain'=>$remain), 'block_content');
        $testingOverView->appendBlockData(array('count'=>$count), 'block_content');
        $testingOverView->appendBlockData(array('totalCount'=>$totalCount), 'block_content');
        $testingOverView->setBlockData("Bài thi","main_header");
        //$testingOverView->setBlockData("Bài thi","block_content");
        $testingOverView->render();
    }

    public function question($classID = null, $number = null)
    {
        $sessionObj = Session::getInstance();
        $studentID = $sessionObj->get('id');

        $testingModel = $this->loadModel('testing');
        $questionObj = $testingModel->getQuestion($classID, $studentID, $number);

        $questionArr = $testingModel->getQuestions($classID, $studentID);

        if ($questionObj == null)
        {
            $this->overview($classID);
            return;
        }

        $count = 0;
        $totalCount = 0;
        foreach ($questionArr as $question)
        {
            $totalCount++;
            if ($question->choice != 0) $count++;
        }

        $tz = new DateTimeZone('Asia/Bangkok');
        $now = new DateTime("now", $tz);
        $results = $testingModel->getResult($classID, $studentID);
        $finishTime = new DateTime($results[0]->finishtime, $tz);
        $remain = $finishTime->diff($now);

        $examInfo = $testingModel->getExamInfo($classID);

        $testingView = $this->loadView('testing');
        $testingView->appendBlockData(array('question'=>$questionObj));
        $testingView->appendBlockData(array('number'=>$number));
        $testingView->appendBlockData(array('classID'=>$classID));
        $testingView->appendBlockData(array('studentID'=>$studentID));

        $testingView->setBlock('block_content', 'info.html', false);
        $testingView->appendBlockData(array('examInfo'=>$examInfo), 'block_content');
        $testingView->appendBlockData(array('classID'=>$classID), 'block_content');
        $testingView->appendBlockData(array('studentID'=>$studentID), 'block_content');
        $testingView->appendBlockData(array('now'=>$now), 'block_content');
        $testingView->appendBlockData(array('count'=>$count), 'block_content');
        $testingView->appendBlockData(array('totalCount'=>$totalCount), 'block_content');
        $testingView->appendBlockData(array('remain'=>$remain), 'block_content');

        $testingView->setBlockData("Làm bài thi","main_header");
        //$testingView->setBlockData("Làm bài thi","block_content");
        $testingView->render();
    }

    public function submit()
    {
        if (! isset($_POST['questionid']))
        {
            $this->index();
            return;
        }
        $questionID = $_POST['questionid'];
        $classID = $_POST['classid'];
        $sessionObj = Session::getInstance();
        $studentID = $sessionObj->get('id');
        $number = $_POST['number'];

        if (isset($_POST['choice']))
        {
            $testingModel = $this->loadModel('testing');
            $message = $testingModel->save($classID, $studentID, $questionID, $_POST['choice']);
            if ($message != null)
            {
                $this->index($message);
                return;
            }
        }

        $this->question($classID, $number);
    }

    public function endtest()
    {
        if (! isset($_POST['classid']))
        {
            $this->index();
            return;
        }
        $classID = $_POST['classid'];
        $sessionObj = Session::getInstance();
        $studentID = $sessionObj->get('id');

        $testingModel = $this->loadModel('testing');
        $message = $testingModel->endtest($classID, $studentID);

        $this->index($message);
    }
}
?>